﻿using System;
using System.Data.Common;
using FlexField.Metadata.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace FlexField.Metadata.EFCore.Tests
{
    public class TestDbContext : MetadataDbContext<Guid>
    {
        private readonly DbConnection _connection;

        protected TestDbContext(DbConnection connection)
        {
            _connection = connection;
        }

        public static TContext Initialize<TContext>(TContext context) where TContext : DbContext
        {
            context.Database.EnsureCreated();

            return context;
        }

        public static TestDbContext Create(DbConnection connection)
            => Initialize(new TestDbContext(connection));

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseSqlite(_connection);
    }
}
